﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Gencode.Gengui.Display
{
    [ToolboxItem(false)]
	public partial class DisplayManager : GUControl
	{
		private Dictionary<string, DisplayableViewControler> leftControls;
		private Dictionary<string, DisplayableView> rightControls;
		string m_currentView;
		DisplayableViewControler leftControl;
		DisplayableView rightControl;

		public DisplayManager()
		{
			InitializeComponent();

			leftControls = new Dictionary<string, DisplayableViewControler>();
			rightControls = new Dictionary<string, DisplayableView>();
			leftControl = new DisplayableViewControler();
			rightControl = new DisplayableView();

			this.Dock = DockStyle.Fill;

			//ViewSplitContainer.Panel1.Controls.Add(leftControl);
			//leftControl.Dock = DockStyle.Fill;
			//ViewSplitContainer.Panel2.Controls.Add(rightControl);
			//rightControl.Dock = DockStyle.Fill;

			//ViewSplitContainer.SplitterDistance = this.Width / 2;

			//AddDisplayControl(new DisplayableViewControler { HasHeader = false, Name = "default", Text = "default" },
			//    new DisplayableView { Name = "defaultVeiw", Text = "default" },
			//    "default");
		}

		public void AddDisplayControl(DisplayableViewControler left, DisplayableView right, string name)
		{
			if (left != null &&
				right != null &&
				!string.IsNullOrEmpty(name))
			{
				if (!leftControls.ContainsKey(name) &&
					!rightControls.ContainsKey(name))
				{
					leftControls.Add(name, left);
					rightControls.Add(name, right);

					if (left.HasHeader)
					{
						left.Header = new ViewSelectionHeader();
						//header.Width = this.viewSelectionPanel.ClientSize.Width - (this.viewSelectionPanel.Margin.Left + this.viewSelectionPanel.Margin.Right);//this.Width - this.Padding.Left - this.Padding.Right;
						//header.Text = left.Text;
						//header.PartnerView = left;

						left.Header.ControlName = name;
						left.Header.ListHeaderCliecked += new ListHeaderClieckEventHandler(HeaderClicked);
						viewSelectionPanel.Controls.Add(left.Header);
						//left.Header.Location = new Point(0, left.Header.Location.Y);
					}
					else
						left.Hide();//hide selectors with no header

					viewSelectionPanel.Controls.Add(left);
					//left.Location = new Point(0, left.Location.Y);
					//left.Dock = DockStyle.Top;
					left.Width = this.viewSelectionPanel.ClientSize.Width - (this.viewSelectionPanel.Margin.Left + this.viewSelectionPanel.Margin.Right);
					if (!left.IsStatic)
						left.Hide();


					left.PartnerView = right;
					right.PartnerView = left;

					if (leftControls.Count == 1 ||
						string.IsNullOrEmpty(CurrentView))
						CurrentView = name;
				}
				else
					throw new Exception("Control or Key is already contained in the manager");
			}
		}

		public string CurrentView
		{
			get
			{
				return m_currentView;
			}
			set
			{
				m_currentView = value;
				if (!string.IsNullOrEmpty(m_currentView))
					SetView();
			}
		}

		/// <summary>
		/// Sets the view with the given index
		/// </summary>
		/// <param name="name">index</param>
		public void SetView(string name)
		{
			CurrentView = name;
		}

		/// <summary>
		/// Sets the default view
		/// </summary>
		public void SetView()
		{
			if(CurrentView == "default" && !rightControls.ContainsKey("default"))
				AddDisplayControl(new DisplayableViewControler { HasHeader = false, Name = "default", Text = "default" },
				new DisplayableView { Name = "defaultVeiw", Text = "default" },
				"default");

			DisplayableViewControler tmpViewControler;
			leftControls.TryGetValue(m_currentView, out tmpViewControler);
			if (tmpViewControler != null)
			{
				int height = this.viewSelectionPanel.Margin.Top + this.viewSelectionPanel.Margin.Bottom;
				foreach (DisplayableViewControler control in leftControls.Values)
				{
					if (!control.IsStatic)
						control.Hide();

					if (control.HasHeader)
						height += control.Header.Height + this.viewSelectionPanel.Margin.Top + this.viewSelectionPanel.Margin.Bottom;
				}

				leftControl = tmpViewControler;

				if (leftControl.HasHeader)
				{
					leftControl.InternalSizeChange += new EventHandler(controls_InternalSizeChange);
					leftControl.Show();
					//leftControl.Dock = DockStyle.Fill;
					if ((this.viewSelectionPanel.ClientSize.Height - height) > 0)
						leftControl.Height = leftControl.CalculateHeight(this.viewSelectionPanel.ClientSize.Height - height);

					if (this.viewSelectionPanel.ClientSize.Width - (this.viewSelectionPanel.Margin.Left + this.viewSelectionPanel.Margin.Right) > 0)
						leftControl.Width = this.viewSelectionPanel.ClientSize.Width - (this.viewSelectionPanel.Margin.Left + this.viewSelectionPanel.Margin.Right);
					if (leftControl.HasHeader)
						leftControl.Header.Width = leftControl.Width;
				}
			}

			DisplayableView tmpView;
			rightControls.TryGetValue(m_currentView, out tmpView);
			if (tmpView != null)
			{
				rightControl = tmpView;
				ViewSplitContainer.Panel2.Controls.Clear();
				ViewSplitContainer.Panel2.Controls.Add(rightControl);
				rightControl.Dock = DockStyle.Fill;
			}
		}

		private void HeaderClicked(object sender, ListHeaderClickEventArgs e)
		{
			if (!string.IsNullOrEmpty(e.ControlName))
				SetView(e.ControlName);
			else
				SetView("default");
		}

		[Browsable(true)]
		public int SplitterDistance
		{
			get
			{
				return ViewSplitContainer.SplitterDistance;
			}
			set
			{
				ViewSplitContainer.SplitterDistance = value;
			}
		}

		public bool IsSplitterFixed
		{
			get
			{
				return ViewSplitContainer.IsSplitterFixed;
			}
			set
			{
				ViewSplitContainer.IsSplitterFixed = value;
			}
		}

		private void viewSelectionPanel_SizeChanged(object sender, EventArgs e)
		{
			if (leftControl.HasHeader)
			{
				int height = this.viewSelectionPanel.Margin.Top + this.viewSelectionPanel.Margin.Bottom;
				foreach (DisplayableViewControler control in leftControls.Values)
				{
					if (control.HasHeader)
						height += control.Header.Height + this.viewSelectionPanel.Margin.Top + this.viewSelectionPanel.Margin.Bottom;
				}

				if ((this.viewSelectionPanel.ClientSize.Height - height) > 0)
					leftControl.Height = leftControl.CalculateHeight(this.viewSelectionPanel.ClientSize.Height - height);
			}
		}

		private void controls_InternalSizeChange(object sender, EventArgs e)
		{
			if (leftControl.HasHeader)
			{
				int height = this.viewSelectionPanel.Margin.Top + this.viewSelectionPanel.Margin.Bottom;
				foreach (DisplayableViewControler control in leftControls.Values)
				{
					if (control.HasHeader)
						height += control.Header.Height + this.viewSelectionPanel.Margin.Top + this.viewSelectionPanel.Margin.Bottom;
				}

				if ((this.viewSelectionPanel.ClientSize.Height - height) > 0)
					leftControl.Height = leftControl.CalculateHeight(this.viewSelectionPanel.ClientSize.Height - height);
			}
		}
	}
}
